<?php
require_once '../libs/app.config.php';
session_start ();
if ($_SESSION ['LoginId'] == false) {
	header ( "Location:login.php" );
}
($charset == 'zh_CN') ? $charset = 'cn' : null;

$getTime = date ( "Y/m/d h:i:s ", time () );
	
if ($_GET ['act'] == 'add') {
	$tracingNo = $orderId;
	$new_string = ereg_replace ( "[^A-Za-z0-9]", ",", $tracingNo );
	$temp = explode ( ',,,,', $new_string );
	$arr = implode ( ',', $temp );
	$temp = explode ( ',,,', $arr );
	$arr = implode ( ',', $temp );
	$temp = explode ( ',,', $arr );
	$final = implode ( ',', $temp );
	$array = explode ( ',', $final );
	$size = count ( $array );
	$validOrderNo = true;
	$addedNo = 0;
	$skippedNo = 0;
	
	if ($size > 0) {
		for($j = 0; $j < $size; $j ++) {
			if ($array [$j] == "") {
				$validOrderNo = false;
				echo "<script>alert('Invalid order: $array [$j], operation cancled!');history.back();</script>";
			}
		}
		if($validOrderNo == true)
		{
			for($j = 0; $j < $size; $j ++) {
				$tpm->query ( "SET AUTOCOMMIT=0");
				$tpm->query ( "START TRANSACTION");
				
				$sql = "select orderId from orders where orderId='" . $array [$j] . "'";
				$query = $tpm->num_rows ( $tpm->query ( $sql ) );
				
				if ($query == 0) {
					$status = "ongoing";
					
					
					$add = array (
							"orderId" => $array [$j],
							"origin" => "悉尼",
							"destination" => "",
							"items" => $items,
							"status" => $status,
							"createTime" => $getTime 
					);
					
					$result = $tpm->insert ( "orders", $add );
					
					if($_POST['datepicker']&& $_POST['location']&&$_POST['detail'])
					{
						$formattedTime = (strlen($datepicker)>12) ? $datepicker : (substr($datepicker,6,4)."-".substr($datepicker,0,2)."-".substr($datepicker,3,2).substr($getTime,10,9));
						$add1 = array (
								"orderId" => $array [$j],
								"time" => $formattedTime,
								"location" => $location,
								"detail" => $detail
						);
						$result1 = $tpm->insert ( "order_records", $add1 );
					}
					if ($result > 0)
						$addedNo = $addedNo + 1;
					else
					{
						$tpm->query ( "ROLLBACK");	 
						echo "<script>alert('Fail to create order: $array [$j], operation cancled !');location.href=('orders_add.php');</script>";
					}
				}
				else
				{
					$skippedNo = $skippedNo + 1;
					echo "<script>alert('Order: {$array [$j]} already exists in database, order skipped!');</script>";
				}					
			}
			$tpm->query("COMMIT");			
			echo "<script>alert('Total {$addedNo} orders added successfully\\nTotal {$skippedNo} orders skipped!');location.href=('ongoing_orders_list.php');</script>";
			
		}
		}
		else 
			echo "<script>alert('Invalid Order Number, operation cancled!');history.back();</script>";
}

$sql2 = "select id, value_type, value_info from setting_value where value_type = 'location' order by value_info";
$query2 = $tpm->query ( $sql2 );
while ( $rows = $tpm->fetch_array ( $query2 ) ) {
	$locations [] = array (
			'id' => $rows ['id'],
			'value_info' => $rows ['value_info'] 
	);
}

$sql5 = "select id, value_type, value_info from setting_value where value_type = 'status' order by value_info";
$query5 = $tpm->query ( $sql5 );
while ( $rows = $tpm->fetch_array ( $query5 ) ) {
	$status [] = array (
			'id' => $rows ['id'],
			'value_info' => $rows ['value_info']
	);
}

// print_r($list);exit();
require_once (INCLUDE_PATH . 'template.inc.php'); // 模板类及对象$tpl的加载
if($_REQUEST ['recordChecked'])
{
	$displayRecord = $_REQUEST ['recordChecked'];
	$tpl->assign ( "displayRecord", $displayRecord );
}
$tpl->assign ( "locations", $locations );
$tpl->assign ( "getTime", $getTime );
$tpl->assign ( "status", $status );
$tpl->assign ( "list", $list );
$tpl->display ( "orders_add.html" );
?>

